package com.whfc.iot.hj.processor;

import com.whfc.common.constant.QueueConst;
import com.whfc.common.enums.DevicePlatform;
import com.whfc.common.enums.DeviceType;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.JSONUtil;
import com.whfc.common.util.WindUtil;
import com.whfc.env.dto.AppDeviceDustLogDTO;
import com.whfc.hj.entity.HjMsg;
import com.whfc.hj.entity.HjMsg2011WC;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: 2011消息处理
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/17 9:17
 */
@Component
public class HjUpMsgProcessor2011 implements HjUpMsgProcessor<HjMsg2011WC> {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public void processMsg(HjMsg2011WC reqMsg) {

        logger.info("处理2011数据:{}", JSONUtil.toString(reqMsg));

        AppDeviceDustLogDTO dustLogDTO = new AppDeviceDustLogDTO();
        dustLogDTO.setDeviceType(DeviceType.dust.getValue());
        dustLogDTO.setPlatform(DevicePlatform.hj212.name());
        dustLogDTO.setDeviceCode(reqMsg.getMn());
        dustLogDTO.setDeviceId(reqMsg.getDeviceId());
        dustLogDTO.setTime(DateUtil.parseDate(reqMsg.getDataTime(), "yyyyMMddHHmmss"));
        if (!StringUtils.isEmpty(reqMsg.getB03())) {
            dustLogDTO.setNoise(Double.valueOf(reqMsg.getB03()));
        }
        if (!StringUtils.isEmpty(reqMsg.getPm10())) {
            dustLogDTO.setPm10(Double.valueOf(reqMsg.getPm10()).longValue());
        }
        if (!StringUtils.isEmpty(reqMsg.getPm25())) {
            dustLogDTO.setPm25(Double.valueOf(reqMsg.getPm25()).longValue());
        }
        if (!StringUtils.isEmpty(reqMsg.getT01())) {
            dustLogDTO.setEnvTemp(Double.valueOf(reqMsg.getT01()));
        }
        if (!StringUtils.isEmpty(reqMsg.getH01())) {
            dustLogDTO.setEnvRh(Double.valueOf(reqMsg.getH01()));
        }
        if (!StringUtils.isEmpty(reqMsg.getW02())) {
            dustLogDTO.setWindSpeed(Double.valueOf(reqMsg.getW02()));
        }
        if (!StringUtils.isEmpty(reqMsg.getW01())) {
            int windDirection = WindUtil.angle2WindDirection(Double.valueOf(reqMsg.getW01()));
            dustLogDTO.setWindDirection(windDirection);
        }
        if (!StringUtils.isEmpty(reqMsg.getTsp())) {
            dustLogDTO.setTsp(Double.valueOf(reqMsg.getTsp()).longValue());
        }
        if (!StringUtils.isEmpty(reqMsg.getAtmos())) {
            dustLogDTO.setAtmos(Double.valueOf(reqMsg.getAtmos()));
        }

        //入消息队列
        String textMsg = JSONUtil.toString(dustLogDTO);
        try {
            amqpTemplate.convertAndSend(QueueConst.ENV_DUST_DATA, textMsg);
        } catch (Exception ex) {
            logger.error("", ex);
        }
    }
}
